package eu.boss.fragments;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import eu.boss.R;
import eu.boss.activities.FragmentsSliderActivity;
import eu.boss.controller.ShadowStatsController;
import eu.boss.model.Player;
import eu.boss.model.Team;

/**
 * This fragment displays teams stats in dedicated fields
 * @author Arnaud
 *
 */
public class TeamStatsFragment extends Fragment {

	private ShadowStatsController mController;
	private TextView bNS; // Best Name Shadow
	private TextView bNH; // Best Name Hunter
	private TextView bNN; // Best Name Neutral
	private TextView bRS; // Best Ratio Shadow
	private TextView bRH; // Best Ratio Hunter
	private TextView bRN; // Best Ratio Neutral
	private TextView SW; // Shadow Wins
	private TextView SL; // Shadow Losts
	private TextView HW; // Hunter wins
	private TextView HL; // Hunter losts
	private TextView NW; // Neutral wins
	private TextView NL; // Neutral losts

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View v = inflater.inflate(R.layout.teamstats, container, false);

		FragmentsSliderActivity sliderActivity = (FragmentsSliderActivity) getActivity();
		mController = sliderActivity.getController();
		// initialize components
		bNS = (TextView) v.findViewById(R.id.best_name_s);
		bNH = (TextView) v.findViewById(R.id.best_name_h);
		bNN = (TextView) v.findViewById(R.id.best_name_n);
		bRS = (TextView) v.findViewById(R.id.best_ratio_s);
		bRH = (TextView) v.findViewById(R.id.best_ratio_h);
		bRN = (TextView) v.findViewById(R.id.best_ratio_n);

		SW = (TextView) v.findViewById(R.id.tvSWs);
		SL = (TextView) v.findViewById(R.id.tvSLs);
		HW = (TextView) v.findViewById(R.id.tvHWs);
		HL = (TextView) v.findViewById(R.id.tvHLs);
		NW = (TextView) v.findViewById(R.id.tvNWs);
		NL = (TextView) v.findViewById(R.id.tvNLs);

		SW = (TextView) v.findViewById(R.id.tvSWs);
		SL = (TextView) v.findViewById(R.id.tvSLs);
		HW = (TextView) v.findViewById(R.id.tvHWs);
		HL = (TextView) v.findViewById(R.id.tvHLs);
		NW = (TextView) v.findViewById(R.id.tvNWs);
		NL = (TextView) v.findViewById(R.id.tvNLs);

		return v;

	}

	/**
	 * Displays results in dedicated fields
	 */
	public void displayResults() {
		String bestS = "";
		String bestH = "";
		String bestN = "";
		float bestRatioS = (float) 0.00;
		float bestRatioH = (float) 0.00;
		float bestRatioN = (float) 0.00;
		Player p = null;

		try {
			for (int i = 0; i < mController.getStatsMessage().getPlayers()
					.size(); i++) {
				p = mController.getStatsMessage().getPlayers().get(i);

				if (Float.valueOf(p.getRatioH().replace(',', '.')) > bestRatioH) {
					bestRatioH = Float.valueOf(p.getRatioH().replace(',', '.'));
					bestH = p.getName();
				}
				if (Float.valueOf(p.getRatioN().replace(',', '.')) > bestRatioN) {
					bestRatioN = Float.valueOf(p.getRatioN().replace(',', '.'));
					bestN = p.getName();
				}
				if (Float.valueOf(p.getRatioS().replace(',', '.')) > bestRatioS) {
					bestRatioS = Float.valueOf(p.getRatioS().replace(',', '.'));
					bestS = p.getName();
				}
			}

			bNS.setText(bestS);
			bNH.setText(bestH);
			bNN.setText(bestN);
			bRS.setText("" + bestRatioS);
			bRH.setText("" + bestRatioH);
			bRN.setText("" + bestRatioN);

			// Get team stats and display it

			List<Team> teamList = mController.getStatsMessage().getTeams();
			for (int i = 0; i < 3; i++) {
				switch (teamList.get(i).getTeam()) {
				case SHADOW:
					SW.setText(""+teamList.get(i).getWins());
					SL.setText(""+teamList.get(i).getLosts());
					break;
				case HUNTER:
					HW.setText(""+teamList.get(i).getWins());
					HL.setText(""+teamList.get(i).getLosts());
					break;
				case NEUTRAL:
					NW.setText(""+teamList.get(i).getWins());
					NL.setText(""+teamList.get(i).getLosts());
					break;
				default:
					break;
				}

			}

		} catch (Exception e) {
			//Log.e("TeamStatsFragment error", e.getMessage());
		}
	}

}
